package com.iexbuy.ihg.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.text.Html;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.iexbuy.ihg.R;
import com.iexbuy.ihg.entity.AppVersion;


public class TwoButtonDialog extends Dialog {
	private Button mRightButton;
	private TextView mMessageView;
	private Context mContext;
	private TextView mSupportVersionAlertView;

	// private TextView mTitleView;
	public TwoButtonDialog(Context context, View.OnClickListener leftOnClickListener, View.OnClickListener rightOnClickListener) {
		super(context, R.style.twoButtonDialog);
		initView(leftOnClickListener, rightOnClickListener);
	}
	
	public TwoButtonDialog(Context context, AppVersion updateInfo, View.OnClickListener leftOnClickListener, View.OnClickListener rightOnClickListener) {
		super(context, R.style.twoButtonDialog);
		this.mContext=context;
		initView(updateInfo,leftOnClickListener, rightOnClickListener);
	}
	
	public TwoButtonDialog(Context context, String mesage, String leftBtnName, String rightBtnName, View.OnClickListener leftOnClickListener, View.OnClickListener rightOnClickListener) {
		super(context, R.style.twoButtonDialog);
		this.mContext=context;
		initView(mesage, leftBtnName, rightBtnName, leftOnClickListener, rightOnClickListener);
	}


	private void initView(View.OnClickListener leftOnClickListener, View.OnClickListener rightOnClickListener) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.two_button_dialog);
		// mTitleView=((TextView)(findViewById(R.id.dialog_title)));
		((Button) (findViewById(R.id.confirm))).setOnClickListener(leftOnClickListener);
		mMessageView = (TextView) findViewById(R.id.dialog_message);
		mRightButton = ((Button) (findViewById(R.id.cancle)));
		mRightButton.setOnClickListener(rightOnClickListener==null?DEFAULT_CLICK_LISTNER:rightOnClickListener);
	}
	private void initView(AppVersion updateInfo,View.OnClickListener leftOnClickListener, View.OnClickListener rightOnClickListener) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.two_button_dialog);
		// mTitleView=((TextView)(findViewById(R.id.dialog_title)));
		((Button) (findViewById(R.id.cancle))).setOnClickListener(leftOnClickListener);
		mMessageView = (TextView) findViewById(R.id.dialog_message);
		mRightButton = ((Button) (findViewById(R.id.confirm)));
		mRightButton.setOnClickListener(rightOnClickListener==null?DEFAULT_CLICK_LISTNER:rightOnClickListener);
		
		String  colon=":";
		String updateLatestVersionName=updateInfo.getVersion();
		String latestVersionName=mContext.getString(R.string.latest_version_name);
		if(updateLatestVersionName!=null) {
			latestVersionName=latestVersionName+colon+updateLatestVersionName;
		}
		
		CharSequence message =Html.fromHtml(String.format("有新版本:"+updateLatestVersionName) + "\n" + updateInfo.getDescription());
		
		setMessage(message);
	   ((Button)(findViewById(R.id.cancle))).setText("取消");
	   ((Button)(findViewById(R.id.confirm))).setText("确认更新");
	}
	private void initView(String mesasge, String leftBtnName, String rightBtnName, View.OnClickListener leftOnClickListener, View.OnClickListener rightOnClickListener) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.two_button_dialog);
		// mTitleView=((TextView)(findViewById(R.id.dialog_title)));
		((Button) (findViewById(R.id.cancle))).setOnClickListener(leftOnClickListener);
		mMessageView = (TextView) findViewById(R.id.dialog_message);
		mRightButton = ((Button) (findViewById(R.id.confirm)));
		mRightButton.setOnClickListener(rightOnClickListener==null?DEFAULT_CLICK_LISTNER:rightOnClickListener);
		
//		String colon=":";
//		String updateLatestVersionName=mesasge;
//		String latestVersionName=mContext.getString(R.string.latest_version_name);
//		if(updateLatestVersionName!=null)
//			latestVersionName=latestVersionName+colon+updateLatestVersionName;
		
		CharSequence message =Html.fromHtml(String.format(mesasge));
		
		setMessage(message);
	   ((Button)(findViewById(R.id.cancle))).setText(leftBtnName);
	   ((Button)(findViewById(R.id.confirm))).setText(rightBtnName);
	}

	public void setMessage(CharSequence message) {
		mMessageView.setText(message);
	}

	public void setTitle(String title) {
		// mTitleView.setText(title);
	}

	public void setRightButton(int visibility) {
		mRightButton.setVisibility(visibility);
	}
	public void setSupportVersionAlert(int visibility){
		mSupportVersionAlertView.setVisibility(visibility);
	}

	private final View.OnClickListener DEFAULT_CLICK_LISTNER = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			dismiss();
		}
	};
}
